#include "NfcProc.h"
#include "PubDef.h"
#include "nfc.h"
#include "pn53x.h"
#include "malloc.h"
#include "mirror-subr.h"
#include "pn532_uart.h"
#include "uart.h"

#define MAX_TARGET_COUNT  16

void NfcProc_Init(nfc_device **pnd)
{    
    pn532_registerFun();
    nfc_init();
    *pnd = nfc_open();
    nfc_initiator_init(*pnd);
}

int NfcProc_NfcList(char *NfcData,nfc_device *pnd,nfc_target *ant)
{
    nfc_modulation nm;
    char res = -1;
    char n;

    memset(ant,0,sizeof(nfc_target));
    
    nm.nmt = NMT_ISO14443A;
    nm.nbr = NBR_106;
    // List ISO14443A targets
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {        
        
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;
        NfcData[2] = ant[0].nti.nai.btSak;

        for (n = 0;n < ant[0].nti.nai.szUidLen;n++)
        {
            NfcData[3+n] = ant[0].nti.nai.abtUid[n];
        }     

        return true;
    }

    nm.nmt = NMT_FELICA;
    nm.nbr = NBR_212;
    // List Felica tags
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {        
        
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;
        NfcData[2] = ant[0].nti.nfi.btResCode;
        NfcData[3] = ant[0].nti.nfi.abtSysCode[0];
        NfcData[4] = ant[0].nti.nfi.abtSysCode[1];
        
        for (n = 0;n < 8;n++)
        {
            NfcData[5+n] = ant[0].nti.nfi.abtId[n];
            NfcData[13+n] = ant[0].nti.nfi.abtPad[n];
        }        

        return true;
    }

    nm.nbr = NBR_424;
    // List Felica tags
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {        
        
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;
        NfcData[2] = ant[0].nti.nfi.btResCode;
        NfcData[3] = ant[0].nti.nfi.abtSysCode[0];
        NfcData[4] = ant[0].nti.nfi.abtSysCode[1];
        
        for (n = 0;n < 8;n++)
        {
            NfcData[5+n] = ant[0].nti.nfi.abtId[n];
            NfcData[13+n] = ant[0].nti.nfi.abtPad[n];
        }        

        return true;
    }

    nm.nmt = NMT_ISO14443B;
    nm.nbr = NBR_106;
    // List ISO14443B targets
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {                
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;        
        NfcData[2] = ant[0].nti.nbi.ui8CardIdentifier;
        NfcData[3] = ant[0].nti.nbi.abtProtocolInfo[0];
        NfcData[4] = ant[0].nti.nbi.abtProtocolInfo[1];
        NfcData[5] = ant[0].nti.nbi.abtProtocolInfo[2];
        
        for (n = 0;n < 4;n++)
        {
            NfcData[6+n] = ant[0].nti.nbi.abtPupi[n];
            NfcData[10+n] = ant[0].nti.nbi.abtApplicationData[n];
        }        

        return true;
    }

    nm.nmt = NMT_ISO14443BI;
    nm.nbr = NBR_106;
    // List ISO14443B targets
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {                
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;        
        NfcData[2] = ant[0].nti.nii.btVerLog;
        NfcData[3] = ant[0].nti.nii.btConfig;       
        
        for (n = 0;n < 4;n++)
        {
            NfcData[4+n] = ant[0].nti.nii.abtDIV[n];
        }        

        for (n = 0;n < ant[0].nti.nii.szAtrLen && 
             ant[0].nti.nii.szAtrLen <= 33;n++)
        {
            NfcData[8+n] = ant[0].nti.nii.abtAtr[n];
        }  
        
        return true;
    }

    nm.nmt = NMT_ISO14443B2SR;
    nm.nbr = NBR_106;
    // List ISO14443B-2 ST SRx family targets
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {                
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;            
        
        for (n = 0;n < 8;n++)
        {
            NfcData[2+n] = ant[0].nti.nsi.abtUID[n];
        }        
        
        return true;
    }

    nm.nmt = NMT_ISO14443B2CT;
    nm.nbr = NBR_106;
    // List ISO14443B-2 ASK CTx family targets
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {                
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;            
        NfcData[2] = ant[0].nti.nci.btProdCode;
        NfcData[3] = ant[0].nti.nci.btFabCode;
            
        for (n = 0;n < 4;n++)
        {
            NfcData[4+n] = ant[0].nti.nci.abtUID[n];
        }        
        
        return true;
    }

    nm.nmt = NMT_JEWEL;
    nm.nbr = NBR_106;
    // List Jewel targets
    if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) > 0)
    {                
        NfcData[0] = nm.nmt;
        NfcData[1] = nm.nbr;            
        NfcData[2] = ant[0].nti.nji.btSensRes[0];
        NfcData[3] = ant[0].nti.nji.btSensRes[1];
            
        for (n = 0;n < 4;n++)
        {
            NfcData[4+n] = ant[0].nti.nji.btId[n];
        }        
        
        return true;
    }

    return false;
   
}

